package com.srwl.mytx.widget;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.srwl.mytx.R;
import com.srwl.mytx.adapter.BottomDialogAdapter;

import java.util.List;

public class BottomDialog extends BottomSheetDialog implements BottomDialogAdapter.OnItemClickListener {
    private InteractionListener listener;
    private List<String> dialogItems;
    RecyclerView rv_dialogItems;
    BottomDialogAdapter dialogAdapter;

    public BottomDialog(@NonNull Context context) {
        super(context);
    }

    public BottomDialog(@NonNull Context context, List<String> dialogItems) {
        super(context, R.style.BottomDialog);
        this.dialogItems = dialogItems;
        init(context, dialogItems);
    }

    public BottomDialog(@NonNull Context context, List<String> dialogItems, final InteractionListener listener) {
        super(context, R.style.BottomDialog);
        this.dialogItems = dialogItems;
        this.listener = listener;
        init(context, dialogItems);
    }

    void init(@NonNull Context context, List<String> dialogItems) {
        setContentView(R.layout.bottom_dialog);
        rv_dialogItems = findViewById(R.id.rv_dialog);
        dialogAdapter = new BottomDialogAdapter(context, dialogItems);
        dialogAdapter.setOnItemClickListener(this);
        rv_dialogItems.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
        rv_dialogItems.setAdapter(dialogAdapter);
    }

    public void setListener(InteractionListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(int position) {
        listener.onDialogItemViewClick(position, dialogItems.get(position));
        dismiss();
    }

    @Override
    public void onCancel() {
        dismiss();
    }

    public interface InteractionListener {
        void onDialogItemViewClick(int position, String itemName);
    }
}
